<template>
  <div class="app-container">
      <el-button type="primary" size="mini" @click="handleAdd()">新增</el-button>
      <el-button type="danger" size="mini" @click="handleDeleteAll()">删除</el-button>
    <el-table ref="multipleTable" :data="list" v-loading="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="公司名称"  width="150">
        <template slot-scope="scope">
          {{scope.row.compName}}
        </template>
      </el-table-column>
      <el-table-column label="代理渠道代码"  width="150">
        <template slot-scope="scope">
          <span>{{scope.row.agentCode}}</span>
        </template>
      </el-table-column>
      <el-table-column label="合作名称"  width="150">
        <template slot-scope="scope">
          <span>{{scope.row.parName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="级别">
        <template slot-scope="scope">
          <span>{{scope.row.level}}</span>
        </template>
      </el-table-column>
      <el-table-column label="套餐类型">
        <template slot-scope="scope">
          <span v-if="scope.row.mealType == 1">默认</span>
          <span v-else-if="scope.row.mealType == 2">其他</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="佣金类型">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{scope.row.commissionType}}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="佣金费率">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{scope.row.commissionRate}}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="状态"  width="80">
        <template slot-scope="scope">
          <span>{{scope.row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间"  width="180">
        <template slot-scope="scope">
          <span>{{parseTime(scope.row.startDate)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间"  width="180">
        <template slot-scope="scope">
          <span>{{parseTime(scope.row.endDate)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            @click="handlePayEdit(scope.$index, scope.row)">支付权限</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 4px;">
      <el-pagination
        background
        layout="total, prev, pager, next"
        :page-size="pageSize"
        :current-page="pageNum"
        :total="pageTotal"
        @current-change="pageChange">
      </el-pagination>
    </div>

    <el-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="企业代码" :label-width="formLabelWidth">
          <el-input v-model="form.agentCode"></el-input>
        </el-form-item>
        <el-form-item label="公司地址" :label-width="formLabelWidth">
          <el-input v-model="form.compAddress"></el-input>
        </el-form-item>
        <el-form-item label="公司名称" :label-width="formLabelWidth">
          <el-input v-model="form.compName"></el-input>
        </el-form-item>
        <!--<el-form-item label="代理类型" :label-width="formLabelWidth">-->
          <!--<el-input v-model="form.type"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="级别" :label-width="formLabelWidth">
          <el-input v-model="form.level"></el-input>
        </el-form-item>
        <!--<el-form-item label="佣金类型" :label-width="formLabelWidth">-->
          <!--<el-input v-model="form.commissionType"></el-input>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="佣金费率" :label-width="formLabelWidth">-->
          <!--<el-input v-model="form.commissionRate"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-input v-model="form.status"></el-input>
        </el-form-item>
        <el-form-item label="合作名称" :label-width="formLabelWidth">
          <el-input v-model="form.parName"></el-input>
        </el-form-item>
        <el-form-item label="套餐类型" :label-width="formLabelWidth">
          <el-select v-model="form.mealType" placeholder="请选择">
            <el-option key="1" value="1" label="默认"></el-option>
            <el-option key="2" value="2" label="其他"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合作开始时间" :label-width="formLabelWidth">
          <el-date-picker v-model="form.startDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>
        <el-form-item label="合作结束时间" :label-width="formLabelWidth">
          <el-date-picker v-model="form.endDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>
        <el-form-item label="电子邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.moblie"></el-input>
        </el-form-item>
        <el-form-item label="邮政编码" :label-width="formLabelWidth">
          <el-input v-model="form.postcode"></el-input>
        </el-form-item>
        <el-form-item label="座机" :label-width="formLabelWidth">
          <el-input v-model="form.tel"></el-input>
        </el-form-item>
        <el-form-item label="首页链接" :label-width="formLabelWidth">
          <el-input v-model="form.indexUrl"></el-input>
        </el-form-item>
        <el-form-item label="合同链接" :label-width="formLabelWidth">
          <el-input v-model="form.contractUrl"></el-input>
        </el-form-item>
        <el-form-item label="了解慧延保链接" :label-width="formLabelWidth">
          <el-input v-model="form.ljzhybUrl"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="支付权限" :visible.sync="dialogPayVisible">
      <el-button type="primary" size="mini" @click="handlePayAdd()">新增</el-button>
      <el-table :data="payList" v-loading="payListLoading" empty-text="暂无数据，默认为全部的支付方式" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column label="公司名称"  width="150">
          <template slot-scope="scope">
            {{form.compName}}
          </template>
        </el-table-column>
        <el-table-column label="代理渠道代码" width="150">
          <template slot-scope="scope">
            <span>{{scope.row.agentCode}}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付方式" width="100">
          <template slot-scope="scope">
            <span>{{scope.row.payType}}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付方式名称" width="150">
          <template slot-scope="scope">
            <span>{{scope.row.payName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handlePayDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPayVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新增支付权限 -->
    <el-dialog title="新增支付权限" :visible.sync="dialogPayAddVisible">
      <el-form>
        <el-form-item label="支付方式" :label-width="formLabelWidth">
          <el-select v-model="payType" placeholder="选择支付方式">
            <el-option v-for="item in paySelect" :key="item.dictCode" :value="item.dictCode" :label="item.dictName"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPayAddVisible = false">取 消</el-button>
        <el-button type="primary" @click="paySubmit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { agentApi } from '@/api/agent'
  import { parseTime } from '@/utils/index'

  export default {
    data() {
      return {
        pageNum: 1, // 当前页
        pageSize: 20, // 每页数
        pageTotal: 0, // 总页数
        list: null,
        listLoading: true,
        dialogFormVisible: false,
        dialogFormTitle: '',
        formLabelWidth: '100px',
        form: {},
        // 支付权限
        payListLoading: true,
        dialogPayVisible: false,
        payList: [],
        dialogPayAddVisible: false,
        paySelect: [],
        payType: ''
      }
    },
    filters: {
    },
    created() {
      this.fetchSelectData()
      this.fetchData()
    },
    methods: {
      fetchSelectData() {
        // 支付类型
        agentApi.listDictByPCode('payType').then(response => {
          this.paySelect = response.data
        })
      },
      fetchData() {
        this.listLoading = true
        agentApi.getList(this.pageNum, this.pageSize).then(response => {
          console.log(response)
          this.list = response.list
          this.listLoading = false
          this.pageTotal = response.total
          this.pageNum = response.pageNum
        })
      },
      handleAdd() {
        this.form = {
          roleIdSelect: []
        }
        this.codeDisabled = false
        this.dialogFormTitle = '新增'
        this.dialogFormVisible = true
      },
      handleDeleteAll() {
        console.log(this.$refs.multipleTable.selection)
        const selectedData = this.$refs.multipleTable.selection
        if (selectedData.length <= 0) {
          this.$message({
            type: 'info',
            message: '请选择至少一行数据'
          })
          return
        }
        let ids = ''
        for (let i = 0; i < selectedData.length; i++) {
          ids += selectedData[i].id + ','
        }
        if (ids.length > 0) {
          ids = ids.substring(0, ids.length - 1)
        }
        this.$confirm('确认删除选中的' + selectedData.length + '条数据', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          agentApi.deleted(ids).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.fetchData()
          })
        })
      },
      handleEdit(index, row) {
        this.form = row
        this.codeDisabled = true
        this.dialogFormTitle = '编辑'
        this.dialogFormVisible = true
      },
      // 时间格式化
      parseTime(time) {
        return parseTime(time, null)
      },
      handleDelete(index, row) {
        this.$confirm('确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          agentApi.deleted(row.id + '').then(response => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.fetchData()
          })
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // })
        })
      },
      submit() {
        console.log(this.form)
        if (this.form.id && this.form.id !== '') {
          // 编辑
          agentApi.update(this.form).then(response => {
            this.dialogFormVisible = false
            this.fetchData()
          })
        } else {
          // 新增
          agentApi.add(this.form).then(response => {
            this.dialogFormVisible = false
            this.fetchData()
          })
        }
      },
      pageChange(val) {
        this.pageNum = val
        this.fetchData()
      },

      // 支付权限设置
      handlePayEdit(index, row) {
        this.form = row
        this.dialogPayVisible = true
        this.fetchPay()
      },
      fetchPay() {
        this.payListLoading = true
        agentApi.getPayType(this.form.id).then(response => {
          this.payList = response.data
          this.payListLoading = false
        })
      },
      handlePayAdd() {
        this.dialogPayAddVisible = true
      },
      paySubmit() {
        console.log(this.payType)
        for (let i = 0; i < this.paySelect.length; i++) {
          if (this.paySelect[i].dictCode === this.payType) {
            const agentPay = {
              agentId: this.form.id,
              agentCode: this.form.agentCode,
              payType: this.paySelect[i].dictCode,
              payName: this.paySelect[i].dictName
            }
            agentApi.addPay(agentPay).then(response => {
              console.log(response)
              if (response.status === '200') {
                this.fetchPay()
                this.dialogPayAddVisible = false
              }
              this.$message({
                type: 'success',
                message: response.message
              })
            })
          }
        }
      },
      handlePayDelete(index, row) {
        this.$confirm('确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          agentApi.deletePay(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.fetchPay()
          })
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // })
        })
      }
    }
  }
</script>
